<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="openAddPermissionDialog(1, '0')"
          >
            添加权限
          </el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">
          <!-- el-table 支持树类型的数据的显示。当 row 中包含 children 字段时，被视为树形数据。渲染树形数据时，必须要指定 row-key -->
          <!-- row-key的值是为了起到唯一作用，这里一般填的是子数据中的id--直接写id就行，默认是子数据中的数据 -->
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="openAddPermissionDialog(2, row.id)"
              >
                添加权限点
              </el-button>
              <el-button size="small" type="text" @click="editPermiss(row.id)">
                查看
              </el-button>
              <el-button
                size="small"
                type="text"
                @click="deletePermiss(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog
      v-loading="loading"
      :visible="showDialog"
      :title="title"
      @close="closeDialog"
    >
      <!-- 表单内容 -->
      <el-form label-width="100px" :model="formData">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitPermiss">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  deletePermissApi,
  editPermissApi,
  reqGetPermissionDetail,
  reqGetPermissionList,
  updatePermissApi
} from '@/api/permission'
import { recursionDepartsFn } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      // 权限点列表
      permissionList: [],
      // 控制添加权限点的显隐
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下,
      },
      // 等待中
      loading: false,
      title: '新增权限'
    }
  },
  computed: {
    // // 弹层的名称
    // title() {
    //   return this.formData.id ? '编辑权限' : '新增权限'
    // }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限点列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log(data)
      // 使用递归方法处理数据--将数据处理成树形数据
      const list = await recursionDepartsFn(data, '0')
      // console.log(list)
      this.permissionList = list
    },
    // 打开添加权限点弹层
    openAddPermissionDialog(type, pid) {
      this.title = '新增权限'
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭弹层
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 点击确认键
    async submitPermiss() {
      if (this.formData.id) {
        // 编辑
        await editPermissApi(this.formData)
      } else {
        await updatePermissApi(this.formData)
      }

      this.$message.success('操作成功')
      this.getPermissionList()
      this.closeDialog()
    },
    // 点击删除键
    deletePermiss(id) {
      this.$confirm('确定删除吗？', '温馨提示')
        .then(async () => {
          await deletePermissApi(id)
          this.getPermissionList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.error('删除失败')
        })
    },
    // 点击查看
    async editPermiss(id) {
      this.loading = true
      const res = await reqGetPermissionDetail(id)
      this.title = '编辑权限'
      this.showDialog = true
      this.loading = false
      // console.log(res)
      this.formData = res.data
    }
  }
}
</script>

<style></style>
